<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('合同编号')" prop="contractId">
            <el-select v-model="form.contractId" filterable :placeholder="$t('合同编号')" :clearable="true">
              <el-option
                v-for="dict in contractIds"
                :key="dict.id"
                :label="$t(dict.contractNo)"
                :value="dict.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('期次')" prop="issue">
            <el-input v-model="form.issue" :placeholder="$t('期次')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('租期')" prop="dateRange">
            <el-date-picker
              v-model="dateRange"
              style="width: 400px"
              type="daterange"
              align="right"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('付款截止日期')" prop="paymentDueDate">
            <el-date-picker
              v-model="form.paymentDueDate"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('付款截止日期')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('期末租金余额') + $t('金额单位')" prop="balanceEndingRent">
            <Money v-model="form.balanceEndingRent" unit="w" :placeholder="$t('期末租金余额') + $t('金额单位')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('固定租金') + $t('金额单位')" prop="deadRent">
            <Money v-model="form.deadRent" unit="w" :placeholder="$t('固定租金') + $t('金额单位')" @change="countTotalAmount" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('浮动租金') + $t('金额单位')" prop="floatingRent">
            <Money v-model="form.floatingRent" unit="w" :placeholder="$t('浮动租金') + $t('金额单位')" @change="countTotalAmount" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('手续费/咨询费') + $t('金额单位')" prop="serviceCharge">
            <Money v-model="form.serviceCharge" unit="w" :placeholder="$t('手续费/咨询费') + $t('金额单位')" @change="countTotalAmount" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('应收总金额') + $t('金额单位')" prop="totalAmountReceivable">
            <Money v-model="form.totalAmountReceivable" unit="w" :placeholder="$t('应收总金额') + $t('金额单位')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('实际收款日期')" prop="actualPayoutDate">
            <el-date-picker
              v-model="form.actualPayoutDate"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('实际收款日期')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('实际收款金额') + $t('金额单位')" prop="actualCollectionRent">
            <Money v-model="form.actualCollectionRent" unit="w" :placeholder="$t('实际收款金额') + $t('金额单位')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('责任人')" prop="userId">
            <UserSelect v-model="form.userId" :placeholder="$t('责任人')" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" :placeholder="$t('备注')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="leasingProject:leaseRentPlan" />
    </div>
  </div>
</template>

<script>
    import { saveDealLeaseRentPlan, getDealLeaseRentPlan, findSelect } from '@/api/deal/leasingProject/dealLeaseRentPlan'
    export default {
        data() {
            return {
              // 合同编号查看条件
              queryParamsContract: {
                dealId: this.$route.query.id
              },
                // 合同编号
                contractIds: [],
                // 期次区间
                dateRange: [],
                // 表单参数
                form: {},
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                contractId: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                  issue: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                leaseStartDate: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                leaseEndDate: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                paymentDueDate: [
                    { required: false, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                balanceEndingRent: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                deadRent: [
                    { required: false, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                floatingRent: [
                    { required: false, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                serviceCharge: [
                    { required: false, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                totalAmountReceivable: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                actualPayoutDate: [
                    { required: false, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                actualCollectionRent: [
                    { required: false, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                userId: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                  dateRange: [
                    { required: true, validator: this.isDateRange, trigger: 'blur' }
                  ]
                }
            }
        },
        created() {
          findSelect(this.queryParamsContract).then(response => {
            this.contractIds = response.rows
          })
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getDealLeaseRentPlan(data.id).then(response => {
                        this.form = response.data
                      this.dateRange = []
                       this.dateRange[0] = this.parseTime(this.form.leaseStartDate)
                       this.dateRange[1] = this.parseTime(this.form.leaseEndDate)
                        this.loading = false
                    }).catch(() => {
                      this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    dealId: this.$route.query.id,
                    contractId: undefined,
                    issue: undefined,
                    leaseStartDate: undefined,
                    leaseEndDate: undefined,
                    paymentDueDate: undefined,
                    balanceEndingRent: undefined,
                    deadRent: undefined,
                    floatingRent: undefined,
                    serviceCharge: undefined,
                    totalAmountReceivable: undefined,
                    actualPayoutDate: undefined,
                    actualCollectionRent: undefined,
                    userId: this.$store.state.user.userId,
                    dateRange: undefined,
                    remark: undefined,
                    delFlag: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm() {
              if (this.dateRange != null && this.dateRange.length > 1) {
                this.form.leaseStartDate = this.dateRange[0]
                this.form.leaseEndDate = this.dateRange[1]
              } else {
                this.form.leaseStartDate = undefined
                this.form.leaseEndDate = undefined
              }
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveDealLeaseRentPlan(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                       this.locationError(document)
                    }
                }).catch(() => {
                    this.loading = false
                })
           },
          formatting(money) {
              if (!money) {
                return 0
              }
          },
          // 计算应收总金额
          countTotalAmount() {
              if (this.form.deadRent && this.form.floatingRent && this.form.serviceCharge) {
                this.form.totalAmountReceivable = Number(this.form.deadRent) + Number(this.form.floatingRent) + Number(this.form.serviceCharge)
              }
          },
          async isDateRange(rule, value, callback) {
            if (this.dateRange === null) {
              callback(new Error(this.$t('必填信息')))
            } else if (this.dateRange.length === 0) {
              callback(new Error(this.$t('必填信息')))
            } else {
              callback()
            }
          }
        }
    }
</script>
